package cn.luoshaocheng.product.service.impl;

import cn.luoshaocheng.basic.exception.BusinessException;
import cn.luoshaocheng.basic.service.IBaiduAiAuditService;
import cn.luoshaocheng.pet.domain.SearchMasterMsg;
import cn.luoshaocheng.product.domain.Product;
import cn.luoshaocheng.product.domain.ProductDetail;
import cn.luoshaocheng.product.mapper.ProductDetailMapper;
import cn.luoshaocheng.product.mapper.ProductMapper;
import cn.luoshaocheng.product.service.IProductService;
import cn.luoshaocheng.basic.service.impl.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 业务实现类：
 */
@Service
public class ProductServiceImpl extends BaseServiceImpl<Product> implements IProductService {
    @Autowired
    ProductMapper productMapper;
    @Autowired
    ProductDetailMapper productDetailMapper;

    @Autowired
    private IBaiduAiAuditService baiduAiAuditService;

    @Override
    public void onsale(List<Long> ids) {
        Date date = new Date();
        productMapper.onsale(ids,date);
    }

    @Override
    public void offsale(List<Long> ids) {
        Date date = new Date();
        productMapper.offsale(ids,date);
    }

    @Override
    public void publish(Product product, Long id) {

    }



    @Override
    public void save(Product product) {
        //审核
        Boolean textBoolean = baiduAiAuditService.textAudit(product.getName());

        if(!textBoolean){
            throw new BusinessException("参数非法");
        }
        productMapper.save(product);
        ProductDetail productDetail = product.getProductDetail();
        if(productDetail!=null){
            productDetail.setProductId(product.getId());
            productDetailMapper.save(productDetail);
        }

    }

    @Override
    public void delete(Long id) {
        super.delete(id);
        productDetailMapper.removeByProduceId(id);
    }

    @Override
    public void update(Product product) {
        productMapper.update(product);
        productDetailMapper.update(product.getProductDetail());
    }
}
